package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.vo.PageBean;
import cn.itcast.service.company.CompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController{
    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list",name = "分页查询所有公司")
    public String findAll(@RequestParam(name = "page",defaultValue = "1") String pageNum, @RequestParam(name = "pageSize",defaultValue = "5") String pageSize, HttpServletRequest request){
        //最开始的为分页的查询所有公司
        /*List<Company> companyList = companyService.findAll();*/
        //pageBean进行分页查询
        /*PageBean pageBean=companyService.findPages(pageNum,pageSize);*/
        PageInfo<Company> pageInfo = companyService.findPages(pageNum, pageSize);
        /* int i=1/0;*/
        request.setAttribute("page",pageInfo);
        return "company/company-list";
    }
    @RequestMapping(value = "/toAdd",name = "跳转到添加公司的界面")
    public String add(){
        return "company/company-add";
    }
    @RequestMapping("/edit")
    public String edit(Company company,String id){
        if(StringUtils.isBlank(id)){
        company.setId(UUID.randomUUID().toString());
        companyService.save(company);
        }else {
            System.out.println(company);
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }
    @RequestMapping(value = "/delete",name = "删除指定公司")
    public String delete(String id){
        companyService.deleteById(id);
        return "redirect:/company/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "编辑公司")
    public String update(String id){
        Company company=companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-add";
    }
}
